﻿using AntUnion.FrameWork.Model.Models;
using AntUnion.FrameWork.Util;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    public class LeverSettingController : BaseController
    {
        private readonly IWebHostEnvironment _hostingEnv;
        private readonly mtContext _context;
        private readonly SmtpSettings _emailSender;

        public LeverSettingController(mtContext context, IWebHostEnvironment hostingEnv, SmtpSettings emailSender) : base(context, hostingEnv)
        {
            _context = context;
            _hostingEnv = hostingEnv;
            _emailSender = emailSender;
        }
        #region 列表
        public IActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public async Task<JsonResult> GetListJson(int page, int limit, string account, string createDate, long login, string orderNo, string Reviewer, int Mothod = -1, int state = -1)
        {
            ListResult res = new ListResult();
            try
            {
                var data = from y in _context.TbLaverages
                           select new
                           {
                               index = y.Id,
                               sort = y.Sort,
                               names = y.Name,
                               leverageRatioValue = y.Laverage,
                               creator = y.CreateDate,
                               createDate = y.CreateDate,
                               state = y.State
                           };

                res.Count = data.Count();
                res.Data = await data.OrderBy(k => k.sort).ThenByDescending(k => k.index).Skip((page - 1) * limit).Take(limit).ToArrayAsync();
                res.Code = 0;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 更新状态设置
        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="intState"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> UpdateState(int id, int intState)
        {
            Result res = new Result();
            try
            {
                TbLaverage tbLaverage = await _context.TbLaverages.FindAsync(id);
                tbLaverage.State = intState;
                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        #endregion

        #region 删除设置
        [HttpPost, ActionName("Delete")]
        public JsonResult Delete(int id)
        {
            Result res = new Result();
            try
            {

                TbLaverage tbLaverage = _context.TbLaverages.Find(id);
                _context.TbLaverages.Remove(tbLaverage);
                res.Code = 0;
                res.Data = _context.SaveChanges();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 新增服务器设置
        public IActionResult Add()
        {
            return View();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> AddSetting(TbLaverage model)
        {
            Result res = new Result();
            try
            {
                TbLaverage tbLaverage = new TbLaverage();
                tbLaverage.Name = model.Name;
                tbLaverage.Laverage = model.Laverage;
                tbLaverage.Creator= User.UserName;
                tbLaverage.CreateDate= DateTimeUtil.Now;
                tbLaverage.Sort = model.Sort;
                tbLaverage.State = model.State;

                _context.Add(tbLaverage);
                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();

                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        #endregion

    }
}
